package com.javaeedev.test.util;

import org.hibernate.Transaction;

import com.javaeedev.util.HibernateUtil;

public abstract class TransactionCallback {

    public final Object execute() throws Exception {
        Transaction tx = HibernateUtil.getCurrentSession().beginTransaction();
        try {
            Object r = doInTransaction();
            tx.commit();
            return r;
        }
        catch(Exception e) {
            tx.rollback();
            throw e;
        }
    }

    protected abstract Object doInTransaction() throws Exception;
}
